//
//  XJTabBar.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/16.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit

class XJTabBar: UITabBar {
    
    //点击按钮执行闭包
    var composeBtnClickClosure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(composeBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     设置按钮的位置
     */
    override func layoutSubviews() {
        
        super.layoutSubviews()
        composeBtn.center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
        
        let itemW = self.frame.width/5
        var index = 0
        //遍历子控件,设置控件的X值
        for value in subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = itemW
                value.frame.origin.x = CGFloat(index) * itemW
                
                index++
                
                if index == 2 {
                    
                    index++
                }
            }
        }
        
    }
    //监听十字按钮的点击事件
    func composeBtnClick(){
    
        composeBtnClickClosure?()
    }

    /// 懒加载十字按钮
    private lazy var composeBtn: UIButton = {
    
        let button = UIButton()
        
        //添加点击事件
        button.addTarget(self, action: "composeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        //按钮背景色
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button" ), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        button.sizeToFit()
        return button
        
    }()
}
